package com.qh.resourcehub.info.user.dao.impl;

import com.qh.resourcehub.info.user.dao.UserDataInfoDao;
import com.qh.resourcehub.info.user.dto.UserDataDto;
import com.qh.resourcehub.utils.XuguUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.sql.*;
import java.util.*;

@Repository("UserDataInfoDao")
public class UserDataInfoDaoImpl implements UserDataInfoDao {
    @Autowired
    private XuguUtils xuguUtils;
    @Override
    public List<Map<String, Object>> queryApplicationUserData(UserDataDto userDataDto) {

        List<Map<String, Object>> result = new ArrayList<>();
        Connection connection = null;
        PreparedStatement pstmtUser = null;

        try {
            connection = xuguUtils.getConnection();
            List<Object> queryParams = new ArrayList<>();

            // 基本查询语句
            StringBuilder sqlQuery = new StringBuilder("SELECT a.NAME,   a.USERCODE,  b.USERCODE, b.CODE, c.DEPTCODE,  b.POSITIONLEVEL,c.DEPTNAME FROM USER_DATA AS a ");
            sqlQuery.append("INNER JOIN EMPLOYEEDEPARTMENTINFO AS b ON a.USERCODE = b.USERCODE ");
            sqlQuery.append("INNER JOIN DEPARTMENT_DATA AS c ON b.CODE = c.DEPTCODE WHERE 1=1 ");
            if ("00".equals(userDataDto.getProcessStatus())){
                //如果是00查询当前用户部门的处级领导列表
                sqlQuery.append(" AND c.DEPTCODE = ? ");
                queryParams.add(userDataDto.getDeptCode());
            } else if ("01".equals(userDataDto.getProcessStatus())) {
                //如果是01查询信息中心部门的处理领导列表
                sqlQuery.append(" AND c.DEPTCODE = ? ");
                queryParams.add("001");
            } else if ("02".equals(userDataDto.getProcessStatus())) {
                //如果是02查询观网处
                sqlQuery.append(" AND c.DEPTCODE = ? ");
                queryParams.add("002");
            }
            // 动态添加 positionLevel = '03' 和 deptCode = '001016028042' 的过滤条件
            sqlQuery.append(" AND b.POSITIONLEVEL = ? ");
            queryParams.add("03");  // POSITIONLEVEL 为 '03'
            // 创建 PreparedStatement
            pstmtUser = connection.prepareStatement(sqlQuery.toString());
            // 设置查询参数
            for (int i = 0; i < queryParams.size(); i++) {
                pstmtUser.setObject(i + 1, queryParams.get(i));
            }
            // 执行查询
            ResultSet rs = pstmtUser.executeQuery();
            while (rs.next()) {
                Map<String, Object> row = new HashMap<>();
                // 获取ResultSet的元数据（列信息）
                ResultSetMetaData metaData = rs.getMetaData();
                int columnCount = metaData.getColumnCount();
                // 遍历所有列并将列名和值添加到Map中
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = metaData.getColumnLabel(i);  // 获取列名
                    Object columnValue = rs.getObject(i);  // 获取列值
                    row.put(columnName, columnValue);
                }
                // 将该行数据添加到结果列表中
                result.add(row);
            }
            return result; // 返回查询结果

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 确保资源关闭
            try {
                if (pstmtUser != null) {
                    pstmtUser.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return Collections.emptyList(); // 如果发生异常，返回空列表
    }
}
